package org.campuscontent.ui;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;

public class Template9 extends TemplateBase
{
	public static int paraID = 9;

	public JLabel jtfQuiz;
	public JLabel jlbQuiz;
	public JTextField jtfAnswer;
	
	public JRadioButton opt1;
	public JRadioButton opt2;
	public JRadioButton hiddenOpt;
	ButtonGroup groupOpt ;

	public Template9(MainFrame mf)
	{
		super(mf);

		sAnswer = new String("");
		jtfQuiz = new JLabel(Config.Tp9Label_DE);
		jtfAnswer = new JTextField("sample");

		jlbQuiz = new JLabel("");
		
		opt1 = new JRadioButton(Config.Tp9Match_DE);
		opt2 = new JRadioButton(Config.Tp9NotMatch_DE);
		hiddenOpt = new JRadioButton("");
		groupOpt = new ButtonGroup();
		groupOpt.add(opt1);
		groupOpt.add(opt2);
		groupOpt.add(hiddenOpt);
		hiddenOpt.setVisible(false);

		jtfQuiz.setBounds(5, 5, 400, 30);
		jlbQuiz.setBounds(5, 5, 400, 30);
		jtfAnswer.setBounds(40,40,150,30);
		opt1.setBounds(220,40,120,20);
		opt2.setBounds(220,63,170,20);
		

		left.add(jtfQuiz);
		left.add(jlbQuiz);
		left.add(jtfAnswer);
		left.add(opt1);
		left.add(opt2);
		
		if (mf.loidMain.equals("-1")|| mf.getCreateTempaletFrom().equals("problemCtrl"))
		{
			sAnswer = new String("none#none#");
			paraConfig= new String("");
		}
		else
		{
			sAnswer = mf.lobject.getSolution();
			paraConfig = mf.lobject.getConfig();
		}
	
		jlbQuiz.setVisible(false);
		initView();
	}

	public void initView()
	{
		mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_TEMPLATE);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
		String[] ansArray = sAnswer.split("#");
		if (ansArray[0].equals("one"))
			opt1.setSelected(true);
		else
			opt1.setSelected(false);
		if (ansArray[1].equals("two"))
			opt2.setSelected(true);
		else
			opt2.setSelected(false);
	}

	public String getAnswer()
	{
		String sTemp="";
		if (opt1.isSelected())
			sTemp = sTemp + "one#";
		else
			sTemp = sTemp + "none#";
		if (opt2.isSelected())
			sTemp = sTemp + "two#";
		else
			sTemp = sTemp + "none#";
		return sTemp;
	}
	
	public String getParaConfig()
	{
		paraConfig = jtfQuiz.getText() + "#" +  jtfAnswer.getText().trim() + "#" ;
		return paraConfig;
	}
	
	public void setParaConfig(String config)
	{
		String[] arr = config.split("#");
		jtfQuiz.setText(arr[0]);
		jtfAnswer.setText(arr[1]);
	}

	public boolean setPreview()
	{
		String answer = getAnswer();
		if (answer.equals("none#none#"))
		{
			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set the answer!");
			return false;
		} else
		{
			mf.lobject.setSolution(answer);
			sAnswer = answer;
		}
		if (!super.setPreview()) return false;
		
		jlbQuiz.setText(jtfQuiz.getText().trim());
		jtfQuiz.setVisible(false);
		jlbQuiz.setVisible(true);
		hiddenOpt.setSelected(true);
		jtfAnswer.setEditable(false);
		return true;
	}

	public boolean exitPreview()
	{
		super.exitPreview();
		jlbQuiz.setVisible(false);
		jtfQuiz.setVisible(true);
		initView();
		jtfAnswer.setEditable(true);
		return true;
	}

	public String checkAns()
	{
		if (getAnswer().equals(mf.lobject.getSolution())) return "True";
		else
			return "Wrong answer";
	}

	public void showSolution()
	{
		super.showSolution();
		String stemp = mf.lobject.getSolution();
		
		String[] ansArray =stemp.split("#");
		if (ansArray[0].equals("one"))
			opt1.setSelected(true);
		else
			opt1.setSelected(false);
		if (ansArray[1].equals("two"))
			opt2.setSelected(true);
		else
			opt2.setSelected(false);
	}
	
	public boolean updateToLObjectNew()
	{
		//first check if the user have set the 
		sAnswer = getAnswer();
		if (sAnswer.equals("none#none#"))
		{
			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set the answer!");
			return false;
		} else
		{
			super.updateToLObjectNew();
			return true;
		}
	}

	
	public int getId()
	{
		return paraID;
	}
	
}
